package com.frxuexi.park2304

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.mvi.BaseViewActivity
import com.frxuexi.park2304.databinding.ActivityWebViewBinding
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

@Route(path = "/app/WebViewActivity")
class WebViewActivity : BaseViewActivity<ActivityWebViewBinding>() {
    var webView:WebView?=null
    override val binding: ActivityWebViewBinding by lazy { ActivityWebViewBinding.inflate(layoutInflater) }

    override fun initView() {
        webView=binding.web
        webView?.apply {
//            loadUrl("https://baijiahao.baidu.com/s?id=1840865762255385192&wfr=spider&for=pc")
            loadUrl("file:///android_asset/test.html")
//            webViewClient=object :WebViewClient(){}
//            webChromeClient=object :WebChromeClient(){}

                settings.setAppCacheEnabled(true)
            //允许js交互
            settings.javaScriptEnabled=true

            //<!--        js调安卓步骤1.-->
            addJavascriptInterface(this@WebViewActivity,"android")
        }

        binding.btnJs.setOnClickListener {
//            安卓调用js，1
            webView?.loadUrl("javascript:harmonyCallJS()")
        }
    }
//<!--        js调安卓步骤2.-->
    @JavascriptInterface
    fun harmonyosAgree():String{
        return "hello"
    }

    override fun onDestroy() {
        super.onDestroy()
        if(webView!=null){
            webView?.clearCache(true)
            webView?.clearHistory()
            webView?.destroy()
            webView=null
        }
    }

}